package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanUserFavouritesReq;
import com.cloud.lan.api.dto.resp.LanUserResp;
import com.cloud.lan.config.cache.LoadingCacheConfiguration;
import com.cloud.lan.dao.LanGoodsMapper;
import com.cloud.lan.dao.LanUserFavouritesMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.model.dataobject.LanGoodsDO;
import com.cloud.lan.entity.model.dataobject.LanUserFavouritesDO;
import com.cloud.lan.service.ILanUserFavouritesService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;
import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ZERO;

/**
 * <p>
 * 问卷调查 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2021-12-28
 */
@Slf4j
@Service
public class LanUserFavouritesServiceImpl extends ServiceImpl<LanUserFavouritesMapper, LanUserFavouritesDO> implements ILanUserFavouritesService {

    @Resource
    LoadingCacheConfiguration cacheConfiguration;
    @Resource
    LanUserFavouritesMapper lanUserFavouritesMapper;
    @Resource
    LanGoodsMapper lanGoodsMapper;

    @Override
    public BaseResponse<Object> collection(String token, LanUserFavouritesReq request) {
        LanUserResp lanUserResp = cacheConfiguration.userInfo.getUnchecked(token);
        LanUserFavouritesDO lanUserFavouritesDO = new LanUserFavouritesDO();
        lanUserFavouritesDO.setUserId(lanUserResp.getUserId());
        lanUserFavouritesDO.setGoodsId(request.getGoodsId());
        Integer count = lanUserFavouritesMapper.selectCount(new LambdaQueryWrapper<LanUserFavouritesDO>()
                .eq(LanUserFavouritesDO::getIsDeleted, false)
                .eq(LanUserFavouritesDO::getGoodsId, request.getGoodsId())
                .eq(LanUserFavouritesDO::getUserId, lanUserResp.getUserId()));
        if (count > ZERO.getKey() && null != request.getStatus()) {
            log.info("更新用户收藏 {}", request);
            lanUserFavouritesDO.setStatus(request.getStatus());
            lanUserFavouritesDO.setUpdateTime(DateUtil.date());
            lanUserFavouritesMapper.update(lanUserFavouritesDO, new LambdaUpdateWrapper<LanUserFavouritesDO>()
                    .eq(LanUserFavouritesDO::getUserId, lanUserFavouritesDO.getUserId())
                    .eq(LanUserFavouritesDO::getGoodsId, lanUserFavouritesDO.getGoodsId()));
            return BaseResponse.success();
        }
        if (count > ZERO.getKey()) {
            log.info("重复收藏 {}", request);
            return BaseResponse.success();
        }
        lanUserFavouritesMapper.insert(lanUserFavouritesDO);
        return BaseResponse.success();
    }

    @Override
    public BaseResponse<Object> getCollection(String token) {
        LanUserResp lanUserResp = cacheConfiguration.userInfo.getUnchecked(token);
        LambdaQueryWrapper<LanUserFavouritesDO> laQw = new LambdaQueryWrapper<>();
        laQw.eq(LanUserFavouritesDO::getStatus, ONE.getKey())
                .eq(LanUserFavouritesDO::getUserId, lanUserResp.getUserId());
        List<LanUserFavouritesDO> listResp = lanUserFavouritesMapper.selectList(laQw);
        listResp.forEach(l -> {
            LanGoodsDO goodsDO = lanGoodsMapper.selectById(l.getGoodsId());
            l.setLanGoodsDO(goodsDO);
        });
        return BaseResponse.success(listResp);
    }

}
